package cn.anecansaitin.industrialization_expansion.common;

import cn.anecansaitin.industrialization_expansion.api.common.event.*;
import cn.anecansaitin.industrialization_expansion.common.item.crafting.ModifiedToolRecipe;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.ModLoader;

import java.util.List;

public class CommonEventHooks {
    public static void fireModifiedToolAssemblePreEvent(ModifiedToolRecipe recipe, NonNullList<ItemStack> modules) {
        MinecraftForge.EVENT_BUS.post(new ToolAssembleEvent.Pre(recipe, modules));
    }

    public static ItemStack fireModifiedToolAssemblePostEvent(ModifiedToolRecipe recipe, NonNullList<ItemStack> modules, ItemStack result) {
        ToolAssembleEvent.Post event = new ToolAssembleEvent.Post(recipe, modules, result);
        MinecraftForge.EVENT_BUS.post(event);
        return event.getResultTool();
    }

    public static ActionResult<ItemStack> fireToolUseEvent(World world, PlayerEntity player, Hand hand) {
        ToolUseEvent event = new ToolUseEvent(world, player, hand);
        MinecraftForge.EVENT_BUS.post(event);
        return event.getUseResult();
    }

    public static boolean fireToolBreakBlockEvent(ItemStack tool, BlockPos pos, PlayerEntity player) {
        ToolBreakBlockEvent event = new ToolBreakBlockEvent(tool, pos, player);
        MinecraftForge.EVENT_BUS.post(event);
        return event.isCanceled();
    }

    public static ActionResultType fireToolUseOnBlockEvent(ItemUseContext context) {
        ToolUseOnBlockEvent event = new ToolUseOnBlockEvent(context);
        MinecraftForge.EVENT_BUS.post(event);
        return event.getActionResult();
    }

    public static void fireModuleRegisterEvent() {
        MinecraftForge.EVENT_BUS.post(new ModuleRegisterEvent());
    }

    public static void fireToolRegisterEvent() {
        MinecraftForge.EVENT_BUS.post(new ToolRegisterEvent());
    }
}
